package com.ruoyi.auth.controller;

import javax.servlet.http.HttpServletRequest;

import com.ruoyi.auth.form.ForgetPasswordBody;
import com.ruoyi.auth.form.PhoneLoginBody;
import com.ruoyi.common.core.utils.SmsVerificationUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.system.api.domain.PhoneRegisterBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.auth.form.LoginBody;
import com.ruoyi.auth.form.RegisterBody;
import com.ruoyi.auth.service.SysLoginService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.JwtUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.auth.AuthUtil;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.model.LoginUser;

/**
 * token 控制
 *
 * @author ruoyi
 */
@RestController
public class TokenController
{
    @Autowired
    private TokenService tokenService;

    @Autowired
    private SysLoginService sysLoginService;
    @Autowired
    private SmsVerificationUtils smsVerificationUtils;

    @PostMapping("login")
    public R<?> login(@RequestBody LoginBody form)
    {
        // 用户登录
        LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword());
        // 获取登录token
        return R.ok(tokenService.createToken(userInfo));
    }

    @DeleteMapping("logout")
    public R<?> logout(HttpServletRequest request)
    {
        String token = SecurityUtils.getToken(request);
        if (StringUtils.isNotEmpty(token))
        {
            String username = JwtUtils.getUserName(token);
            // 删除用户缓存记录
            AuthUtil.logoutByToken(token);
            // 记录用户退出日志
            sysLoginService.logout(username);
        }
        return R.ok();
    }

    @PostMapping("refresh")
    public R<?> refresh(HttpServletRequest request)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        if (StringUtils.isNotNull(loginUser))
        {
            // 刷新令牌有效期
            tokenService.refreshToken(loginUser);
            return R.ok();
        }
        return R.ok();
    }

    @PostMapping("register")
    public R<?> register(@RequestBody RegisterBody registerBody)
    {
        // 用户注册
        sysLoginService.register(registerBody.getUsername(), registerBody.getPassword());
        return R.ok();
    }
    /**
     * 手机注册用户信息
     */
    @PostMapping("phoneRegister")
    public R<Boolean> phoneRegister(@RequestBody PhoneRegisterBody phoneRegisterBody)
    {
        // 用户注册
        sysLoginService.phoneRegister(phoneRegisterBody);
        return R.ok();
    }

    /**
     * 忘记密码
     */
    @PostMapping("forgetPassword")
    public R<Boolean> forgetPassword(@RequestBody ForgetPasswordBody forgetPasswordBody)
    {
        // 用户注册
        sysLoginService.forgetPassword(forgetPasswordBody);
        return R.ok();
    }

    /**
     * 手机登录
     */
    @PostMapping("phoneLogin")
    public R<?> phoneLogin(@RequestBody PhoneLoginBody form)
    {
        // 用户登录
        LoginUser userInfo = sysLoginService.phoneLogin(form.getPhone(), form.getCode(),form.getUuid());
        // 获取登录token
        return R.ok(tokenService.createToken(userInfo));
    }

    /**
     * 考试登录
     */
    @PostMapping("examLogin")
    public R<?> examLogin(@RequestBody PhoneLoginBody form)
    {
        // 用户登录
        LoginUser userInfo = sysLoginService.examLogin(form.getPhone(), form.getCode(),form.getUuid(), form.getName(), form.getIdNumber(),form.getTrainingClassId());
        // 获取登录token
        return R.ok(tokenService.createToken(userInfo));
    }

    /**
     * 成绩/证书查询登录
     */
    @PostMapping("gradeOrCertificateLogin")
    public R<?> gradeOrCertificateLogin(@RequestBody PhoneLoginBody form)
    {
        // 用户登录
        LoginUser userInfo = sysLoginService.gradeOrCertificateLogin(form.getPhone(), form.getCode(),form.getUuid(), form.getName(), form.getIdNumber());
        // 获取登录token
        return R.ok(tokenService.createToken(userInfo));
    }


    /**
     *  发送验证码
     */
    @GetMapping("sendVerificationCode")
    public AjaxResult send(@RequestParam String phone, @RequestParam String uuid) {
        String send = smsVerificationUtils.send(phone,uuid);
        if ("OK".equals(send)) {
            return AjaxResult.success();
        }
        return AjaxResult.error("短信验证码发送失败", send);
    }
}
